package com.tengju.bff.interfaces.manage.crm.chief;

import com.tengju.bff.interfaces.manage.crm.chief.facade.ChiefGenSecRelationFacade;
import com.tengju.bff.interfaces.manage.crm.chief.form.ChiefGenSecRelationForm;
import com.tengju.bff.interfaces.manage.crm.chief.form.RemoveChiefSecRelationForm;
import com.tengju.bff.interfaces.manage.crm.chief.form.UpdateChiefGenSecRelationForm;
import com.tengju.bff.interfaces.manage.crm.chief.vo.GetChiefGenSecRelationForm;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.StringConvertUtil;
import com.tengju.bff.interfaces.shared.TableVO;
import com.tengju.bff.interfaces.shared.servlet.ManageEntrance;
import com.tengju.bff.interfaces.shared.servlet.ManageUtil;
import com.tengju.support.domain.annotation.BizLog;
import com.tengju.support.domain.annotation.BizLogSysName;
import com.tengju.user.application.chief.ChiefGenSecRelationService;
import com.tengju.user.application.shared.ApplicationException;
import com.tengju.user.domain.model.chief.ChiefId;
import com.tengju.user.domain.model.user.Staff;
import com.tengju.user.domain.shared.DomainException;
import com.tengju.user.domain.shared.ResultCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author: dylan
 * @date: 2021/7/26 2:54 下午
 */
@Validated
@RestController
@Api(tags = "总监SEC关系管理")
@RequestMapping(value = "manage/crm/chief/gen/sec/relation")
@Slf4j
@ManageEntrance
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class ChiefGenSecRelationController {

    private final ChiefGenSecRelationService chiefGenSecRelationService;
    private final ChiefGenSecRelationFacade chiefGenSecRelationFacade;

    @ApiOperation(value = "查询总监Sec关系", notes = "查询总监Sec关系")
    @RequestMapping(value = "queryChiefSecRelationByPage", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<TableVO> queryChiefSecRelationByPage(GetChiefGenSecRelationForm getChiefGenSecRelationForm) {
        try {
            return ApiResponse.newSuccess(chiefGenSecRelationFacade.queryChiefSecRelationByPage(getChiefGenSecRelationForm));
        } catch (DomainException | ApplicationException e) {
            log.error("总监sec关系查询失败,getChiefSecRelationByPage:{}", getChiefGenSecRelationForm, e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }
    }

    @ApiOperation(value = "获取SecIdCode列表", notes = "获取SecIdCode列表")
    @RequestMapping(value = "getSecIdCodeList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<String> getSecIdCodeList(@NotNull(message = "chiefId不能为空") @RequestParam("chiefId") Long chiefId) {
        try {
            return ApiResponse.newSuccess(chiefGenSecRelationFacade.getSecIdCodeList(new ChiefId(chiefId)));
        } catch (DomainException | ApplicationException e) {
            log.error("SecIdCode列表获取失败,chiefId:{}", chiefId, e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }
    }

    @BizLog(sysName = BizLogSysName.TENGJU_CHIEF_MANAGE)
    @ApiOperation(value = "修改总监Sec关系", notes = "修改总监Sec关系")
    @RequestMapping(value = "updateChiefSecRelation", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> updateChiefSecRelation(@Valid @RequestBody UpdateChiefGenSecRelationForm updateChiefGenSecRelationForm) {
        Staff staff = ManageUtil.getOperateUser();
        List<Long> genSecIdCodes = StringConvertUtil.stringConvertToLongList(updateChiefGenSecRelationForm.getSecIdCodes());
        ChiefId chiefId = new ChiefId(updateChiefGenSecRelationForm.getChiefId());
        try {
            return ApiResponse.newSuccess(chiefGenSecRelationService.updateChiefSecRelation(staff, chiefId, genSecIdCodes));
        } catch (DomainException | ApplicationException e) {
            log.error("修改总监sec关系失败,updateChiefSecRelationForm:{}", updateChiefGenSecRelationForm, e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }
    }

    @BizLog(sysName = BizLogSysName.TENGJU_CHIEF_MANAGE)
    @ApiOperation(value = "添加总监Sec关系", notes = "添加总监Sec关系")
    @RequestMapping(value = "addChiefSecRelation", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> addChiefSecRelation(@Valid @RequestBody ChiefGenSecRelationForm chiefGenSecRelationForm) {
        Staff staff = ManageUtil.getOperateUser();
        return ApiResponse.newSuccess(chiefGenSecRelationService.addChiefSecRelation(staff, new ChiefId(chiefGenSecRelationForm.getChiefId()),
                StringConvertUtil.stringConvertToLongList(chiefGenSecRelationForm.getSecIdCodes())));
    }

    @BizLog(sysName = BizLogSysName.TENGJU_CHIEF_MANAGE)
    @ApiOperation(value = "删除总监Sec关系", notes = "删除总监Sec关系")
    @RequestMapping(value = "removeChiefSecRelation", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> removeChiefSecRelation(@RequestBody RemoveChiefSecRelationForm form) {
        try {
            return ApiResponse.newSuccess(chiefGenSecRelationService.removeChiefSecRelation(new ChiefId(form.getChiefId())));
        } catch (DomainException | ApplicationException e) {
            log.error("删除总监sec关系失败,RemoveChiefSecRelationForm:{}", form, e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }
    }
}
